﻿/*-------------------------------------------------------------------------
* 命名空间: WaferSelectV3.ViewModels.SetParamsViewModels/SlaveSettingsViewModel
* 类       名: SlaveSettingsViewModel
* 功      能:  TD
* 时      间:  2024-05-13 10:41:45
* 版      本:  V1.0
* 作      者:  Fioman
* 格      言:  Talk is cheap,show me the code ^_^
*-------------------------------------------------------------------------*/
using Microsoft.IdentityModel.Logging;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Documents;
using WaferSelectV3.Common;
using WaferSelectV3.DAL.DataEntity;
using WaferSelectV3.DAL.LocalDataAccess;
using WaferSelectV3.EventsManager;
using WaferSelectV3.Extensions;
using WaferSelectV3.Models;

namespace WaferSelectV3.ViewModels.SetParamsViewModels
{
    public class SlaveSettingsViewModel : BindableBase
    {
        private readonly IEventAggregator _eventAggregator;
        public SlaveSettingsViewModel(IEventAggregator eventAggregator)
        {
            SlaveParams = new ObservableCollection<SlaveStateModel>();
            PageTitle = "从站通信参数设置: 双击选项进行编辑,Enter结束编辑,然后点击保存";
            ReloadSlaveParams();
            SaveSlaveParamsCommand = new DelegateCommand(DoSaveSlaveParams);

            _eventAggregator = eventAggregator;
            _eventAggregator.GetEvent<SetDialogCloseEvent>().Subscribe(() =>
            {
                // 做清理工作,将相关变量置位null
                SlaveParams = null;
                PageTitle = null;
                SaveHint = null;
            });
        }


        /// <summary>
        /// 保存从站通信参数按钮
        /// </summary>
        public DelegateCommand SaveSlaveParamsCommand { get; set; }
        private async void DoSaveSlaveParams()
        {
            List<SlaveParamsEntity> updateDatas = new List<SlaveParamsEntity>();
            foreach (var data in SlaveParams!)
            {
                var temp = new SlaveParamsEntity()
                {
                    Id = data.Id,
                    SlaveName = data.SlaveName,
                    SlaveIp = data.SlaveIp,
                    SlavePort = data.SlavePort,
                };
                updateDatas.Add(temp);
            }
            var result = SlaveParamsAccess.UpdateDataMany(updateDatas);
            if (result.State)
            {
                SaveHint = "保存成功";
                await Task.Delay(2000);
                SaveHint = null;
            }
            else
            {
                SaveHint = null;
                MessageBox.Show(result.Message!, "数据库操作失败", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            ReloadSlaveParams();
        }

        private void ReloadSlaveParams()
        {
            try
            {
                var result = SlaveParamsAccess.GetSlaveParams();
                if (result.State)
                {
                    SlaveParams?.Clear();
                    foreach (var param in result.Data!)
                    {
                        SlaveParams?.Add(new SlaveStateModel()
                        {
                            Id = param.Id,
                            SlaveName = param.SlaveName,
                            SlaveIp = param.SlaveIp,
                            SlavePort = param.SlavePort,
                        });
                    }
                }
                else
                {
                    LoggerHelper.WriteLogMain(result.Message!, LogType.Fatal);
                }
            }
            catch (Exception ex)
            {
                LoggerHelper.WriteLogMain(ex.GetFullException(), LogType.Fatal);
            }
        }

        public string? PageTitle { get; set; }

        /// <summary>
        /// 保存消息后的提示消息
        /// </summary>
        private string? _saveHint;

        public string? SaveHint
        {
            get { return _saveHint; }
            set
            {
                _saveHint = value;
                RaisePropertyChanged();
            }
        }


        private ObservableCollection<SlaveStateModel>? _slaveParams;

        public ObservableCollection<SlaveStateModel>? SlaveParams
        {
            get { return _slaveParams; }
            set
            {
                _slaveParams = value;
                RaisePropertyChanged();
            }
        }

    }
}
